
function PunchJob(uri) {
  if (!uri)
    uri = BasePath.get() + "/livcos.org/WorkLog/web/Punch.job";
  PunchJob._super.call(this, uri);
  this.data = "";
}
Object.extend("PunchJob", "Resource");

PunchJob.prototype.addData = function(key, value) {
  this.data += "&"+key+"="+encodeURIComponent(value);
}

PunchJob.prototype.setTime = function(minutesAgo, defaultValue) {
  var date;
  if (typeof(minutesAgo) == "number")
    date = new Date(Date.now()-minutesAgo*60*1000);
  else if (defaultValue)
    date = new Date(defaultValue);
  else
    date = new Date();
  this.addData("time", date.toXsDateTimeString());
}

PunchJob.prototype.executeAction = function(action) {
	this.send(action+"=1"+this.data);
}

PunchJob.prototype.onCompleted = function() {
  var log = document.getElementById("log");
  if (log)
    log.innerHTML = this.text;
}

function punchIn(form, minutesAgo) {
  var job = new PunchJob();
  job.setTime(minutesAgo, form.elements["time"].value);
  job.addData("ref", form.elements["ref"].value);
  job.executeAction("in");
}

function punchOut(data, minutesAgo) {
  var job = new PunchJob();
  if (data.elements) {
    job.setTime(minutesAgo, data.elements["time"].value);
    job.addData("ref", data.elements["ref"].value);
  }
  else {
    job.setTime(minutesAgo);
    job.addData("ref", data);
  }
  job.executeAction("out");
}

function punchBreak(form, duration, minutesAgo) {
  var job = new PunchJob();
  job.setTime(minutesAgo, form.elements["time"].value);
  job.addData("duration", duration);
  job.executeAction("break");
}

function punchConnect() {
  var job = new PunchJob();
  job.setTime();
  job.executeAction("connect");
}

function punchDisconnect() {
  var job = new PunchJob();
  job.setTime();
  job.executeAction("disconnect");
}

function deleteLog(id) {
  var job = new PunchJob();
  job.addData("id", id);
  job.executeAction("delete");
}

